//
//  WPHotTradeViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/3/31.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPHotTradeViewController.h"
#import "WPNewsDealingModel.h"
#import "WPNewsDealingTableViewCell.h"
#import "WPNewsPreheatModel.h"
#import "WPNewsPreheatTableViewCell.h"
#import "WPTradeListViewController.h"
#import "WPTradeDetailViewController.h"

@interface WPHotTradeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView * headView;/**< 交易人数背景图 */
@property (nonatomic, strong) UILabel * tradeCountIntrLabel;/**< 交易人数总计说明 */
@property (nonatomic, strong) UILabel * tradeCountLabel;/**< 交易人数总计 */
@property (nonatomic, strong) UITableView * hotTradeTableView;/**< 交易热区表格视图 */
@property (nonatomic, strong) NSMutableArray * hotTradeArr;/**< 交易热区数据源 */
@property (nonatomic, strong) NSArray * detailImageNames;/**< 详情图片数组 */

- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
- (void)refreshData;/**< 刷新数据 */



@end

@implementation WPHotTradeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeDataSource {
    self.detailImageNames = @[@"rain项目详情", @"bigbang项目详情", @"迪斯尼项目详情"];
    self.hotTradeArr = [NSMutableArray arrayWithCapacity:0];
    WPNewsPreheatModel * model = [WPNewsPreheatModel new];
    model.image = @"image1";
    model.name = @"BIGBANG “WORLDTOUR[MADE]” 亚洲巡演";
    model.detailDescriiption = @"BIGBANG五人全体三巡首站上海.南京.深圳.北京.广州.天津.武汉.厦门.西安···入伍前全体最后一次巡演...";
    model.involvementNumber = @(28);
    NSString * bigbangTime = @"20160616";
    NSString * disiniTime = @"20160701";
    NSString * rainTime = @"20160423";
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyyMMdd";
    NSDate * date = [formatter dateFromString:bigbangTime];
    NSDate * disiniDate = [formatter dateFromString:disiniTime];
    NSDate * rainDate = [formatter dateFromString:rainTime];
    NSTimeInterval bigBangInterval = [date timeIntervalSince1970];
    NSTimeInterval disiniInterval = [disiniDate timeIntervalSince1970];
    NSTimeInterval rainInterval = [rainDate timeIntervalSince1970];
    model.endDate = @(bigBangInterval*1000);
    WPNewsPreheatModel * model1 = [WPNewsPreheatModel new];
    model1.image = @"image2";
    model1.name = @"上海迪斯尼乐园6月16日开园庆典";
    model1.detailDescriiption = @"作为中国大陆首座迪尼斯主题乐园，上海迪尼斯乐园将为游客提供无限可能，创造值得珍藏一生的回忆...";
    model1.involvementNumber = @(36);
    model1.endDate = @(disiniInterval*1000);
    
    WPNewsDealingModel * model2 = [WPNewsDealingModel new];
    model2.posterImageName = @"image3";
    model2.title = @"“The squall” Rain亚洲演唱会";
    model2.detail = @"2016“The Squall”王者归来Rain亚洲巡回演唱会-成都站。喊了那么久，终于Rain来了!迎风...";
    model2.ticketCount = @"12";
    model2.allTicketCount = @"30";
    model2.joinCount = @"42";
    model2.leftDays = @(rainInterval*1000);
//    [_hotTradeArr addObject:model1];
//    [_hotTradeArr addObject:model];
}
- (void)initializeUserInterface {
    self.navigationController.navigationBarHidden = NO;
    [self.view addSubview:self.hotTradeTableView];
    __weak typeof(self)weakself = self;
    _hotTradeTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself refreshData];
    }];
    _hotTradeTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakself refreshData];
    }];
    [_hotTradeTableView.header beginRefreshing];
}
- (void)refreshData {
    if (_hotTradeTableView.header.isRefreshing) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_hotTradeTableView.header endRefreshing];
        });
    }
    if (_hotTradeTableView.footer.isRefreshing) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_hotTradeTableView.footer noticeNoMoreData];
        });
    }
}
#pragma mark - 协议UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _hotTradeArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != 0) {
        WPNewsPreheatTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [[WPNewsPreheatTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"123"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        [cell setModelWithModel:_hotTradeArr[indexPath.section]];
        return cell;
    }
    WPNewsDealingTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[WPNewsDealingTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"1234"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    [cell setModelWithModel:_hotTradeArr[indexPath.section]];
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 280*DHFlexibleHorizontalMutiplier();
}
//用户点击了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        WPTradeListViewController * vc = [WPTradeListViewController new];
        vc.model = _hotTradeArr[indexPath.section];
        [self pushToNextController:vc withVCTitle:@"交易期"];
    }
    else if(indexPath.section < 2){
        WPTradeDetailViewController * detailVC = [WPTradeDetailViewController new];
        detailVC.imageName = _detailImageNames[indexPath.section];
        [self pushToNextController:detailVC withVCTitle:@"项目详情"];
    }
}
#pragma mark - getter
- (UIView *)headView {
    if (!_headView) {
        _headView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 70*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            [view addSubview:self.tradeCountIntrLabel];
            [view addSubview:self.tradeCountLabel];
            view;
        });
    }
    return _headView;
}
- (UILabel *)tradeCountIntrLabel {
    if (!_tradeCountIntrLabel) {
        _tradeCountIntrLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 10*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 20*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:16*DHFlexibleHorizontalMutiplier()];
            lab.textAlignment = NSTextAlignmentCenter;
            UIView * leftLine = [[UIView alloc]initWithFrame:CGRectMake(60*DHFlexibleHorizontalMutiplier(), CGRectGetMidY(lab.bounds), 50*DHFlexibleHorizontalMutiplier(), 1)];
            leftLine.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            UIView * rightLine = [[UIView alloc]initWithFrame:CGRectMake(210*DHFlexibleHorizontalMutiplier(), CGRectGetMidY(lab.bounds), 50*DHFlexibleHorizontalMutiplier(), 1)];
            rightLine.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            [lab addSubview:leftLine];
            [lab addSubview:rightLine];
            lab.text = @"累计交易人数";
            lab;
        });
    }
    return _tradeCountIntrLabel;
}
- (UILabel *)tradeCountLabel {
    if (!_tradeCountLabel) {
        _tradeCountLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 30*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:16*DHFlexibleHorizontalMutiplier()];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.textColor = COLOR_RGB(234, 27, 47, 1);
            lab.text = @"1 2 4 8 2 6 7";
            lab;
        });
    }
    return _tradeCountLabel;
}
- (UITableView *)hotTradeTableView {
    if (!_hotTradeTableView) {
        _hotTradeTableView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, navBar_Fheight, SCREEN_SIZE.width, SCREEN_SIZE.height - navBar_Fheight) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableHeaderView = self.headView;
            tableview.pagingEnabled = NO;
            tableview.separatorColor = [UIColor clearColor];
            tableview;
        });
    }
    return _hotTradeTableView;
}

@end
